package com.qf.blogsystem.dao.impl;

import com.qf.blogsystem.core.utils.DruidUtils;
import com.qf.blogsystem.dao.AdminDao;
import com.qf.blogsystem.entity.Admin;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.Date;
import java.util.List;

/**
 * @author Crazy
 * @version 1.0
 * @description: 管理员Dao实现类
 * @date 2022/12/5 15:02
 */
public class AdminDaoImpl implements AdminDao {
    /**
     * 创建DButils执行对象
     */
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());

    /**
     * 添加管理员信息
     * @param admin
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer insert(Admin admin) throws Exception {
        admin.setStatus(1);
        admin.setCreateTime(new Date());
        String sql = "insert into t_admin (account,password,sex,email,createTime,birthday,status) value(?,?,?,?,?,?,?)";
        return queryRunner.update(sql, admin.getAccount(), admin.getPassword(), admin.getSex(), admin.getEmail(), admin.getCreateTime(), admin.getBirthday(), admin.getStatus());
    }

    /**
     * 根据id查询管理员信息
     * @param id
     * @return Admin
     * @throws Exception
     */
    @Override
    public Admin selectById(Integer id) throws Exception {
        String sql = "select * from t_admin where id = ?";
        return queryRunner.query(sql, new BeanHandler<Admin>(Admin.class), id);
    }

    /**
     * 根据id修改管理员信息
     * @param admin
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer updateById(Admin admin) throws Exception {
        String sql = "update t_admin set account = ?,sex = ?,email = ?,birthday = ? where id = ?";
        return queryRunner.update(sql, admin.getAccount(),  admin.getSex(), admin.getEmail(), admin.getBirthday(), admin.getId());
    }

    /**
     * 根据id删除管理员信息
     * @param id
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer deleteById(Integer id) throws Exception {
        String sql = "update t_admin set status = 0 where id = ? and status = 1";
        return queryRunner.update(sql,  id);
    }

    /**
     * 查询全部管理员信息
     * @return List<Admin>
     * @throws Exception
     */
    @Override
    public List<Admin> selectList() throws Exception {
        String sql = "select * from t_admin where status = 1";
        return queryRunner.query(sql,new BeanListHandler<Admin>(Admin.class));
    }

    /**
     * 分页删除管理员信息
     * @param ids
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer batchDelete(List<Integer> ids) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update t_admin set status = 0 where id in(");
        for (int i = 0; i < ids.size(); i++) {
            if (i != ids.size() - 1) {
                stringBuilder.append("?,");
            } else {
                stringBuilder.append("?");
            }
        }
        stringBuilder.append(") and status = 1");
        System.out.println(stringBuilder.toString());
        return queryRunner.update(stringBuilder.toString(), ids.toArray());
    }
}
